function X_stemmed = stemmer(X, vocab)
%
%  STEMMER
%  Usage:  X_stemmed = stemmer(X, vocab)
%
%  Takes in an example matrix X (N examples x M features) with word counts
%  of words in the vocab and returns the stemmed version of the matrix 
%  with features with the same stem combined (sparse).  There is no 
%  guarantee about the order of the combined features in the output as 
%  related to the input features.
%
%  Michael Posner
%  CIS 520
%  Final Project
%  Bayesd and Confused

%create a cell array of stems corresponding to the vocab array
stems = cell(1,size(vocab,2));

for i=1:size(vocab,2)
    stems{i} = porterStemmer(lower(vocab{i}));
end

% now find similar stems and combine their features in Xt_lyrics
ustems = unique(stems);
X_stemmed = zeros(size(X,1),numel(ustems));

for i=1:numel(ustems)
    
    %find indices of all words that have same stem
    l = strcmpi(ustems{i},stems);
    ind = find(l);
    
    %combine those features into one feature vector in the new Xt
    X_stemmed(:,i) = sum(full(X(:,ind)),2);  
    
end

%make sparse
X_stemmed = sparse(X_stemmed);

end